﻿/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace GenerateurDeNombre
{
    using System;
    using System.Windows.Forms;

    /// <summary>
    /// Générateur de nombre à la suite par Nidel
    /// 29/01/2009
    /// http://www.freya.fr
    /// </summary>
    public partial class Generateur : Form
    {
        public Generateur()
        {
            InitializeComponent();
        }

        private void OnEndChanged(object sender, EventArgs e)
        {
            if(txtDebut.Text.Length <= 0)
            {
                lblError.Text = "Entrez le nombre du début.";
                lblError.Visible = true;
            }
            else if(txtFin.Text.Length <= 0)
            {
                lblError.Text = "Entrez le nombre de fin.";
                lblError.Visible = true;
                return;
            }

            int debut;
            int fin;
            if(!Int32.TryParse(txtDebut.Text, out debut))
            {
                lblError.Text = "Nombres de début incorrecte.";
                lblError.Visible = true;
                return;
            }

            if(!Int32.TryParse(txtFin.Text, out fin))
            {
                lblError.Text = "Nombres de fin incorrecte.";
                lblError.Visible = true;
                return;
            }

            int diff = debut - fin;
            if(diff > 200 || diff < -200)
            {
                lblError.Text = "T'as vraiment besoin de tout ça ?! Alors contacte\nl'auteur pour un don Paypal pour qu'il t'envoi\nune version illimitée §§.. ou pas.";
                lblError.Visible = true;
                return;
            }

            if (lblError.Text.Length > 0 || lblError.Visible)
            {
                lblError.Text = "";
                lblError.Visible = false;
            }
            Generate(debut, fin);
        }

        private void Generate(int debut, int fin)
        {
            string nbrs = string.Empty;
            string[] ignore = txtIgnore.Text.Split(';');
            if(debut < fin)
            {
                for (int i = debut; i <= fin; i++)
                {
                    Nbr(ref nbrs, i, ignore);
                }
            }
            else
            {
                for(int i = debut; i >= fin; i--)
                {
                    Nbr(ref nbrs, i, ignore);
                }
            }
            txtFinal.Text = nbrs;
        }

        private static void Nbr(ref string nbr, int i, string[] ignore)
        {
            bool ignored = false;
            for(int j = 0; j < ignore.Length; j++)
            {
                if(ignore[j] == i.ToString())
                {
                    ignored = true;
                    break;
                }
            }
            if(!ignored)
            {
                nbr += i + ";";
            }
        }

        private void OnMouseEnter(object sender, EventArgs e)
        {
            TextBox box = sender as TextBox;
            if (box != null)
            {
                box.BackColor = System.Drawing.Color.LawnGreen;   
            }
        }

        private void OnMouseLeaver(object sender, EventArgs e)
        {
            TextBox box = sender as TextBox;
            if (box != null)
            {
                box.BackColor = System.Drawing.Color.GreenYellow;   
            }
        }
    }
}